package diona.search

import org.joda.time.DateTime

/**
 * User: anton
 * Date: 03.07.11 13:50
 */
class PostInfo(val url: String,
               val tstamp: DateTime,
               val isComment: Boolean,
               val snippet: Option[String] = None,
               val author: Option[String] = None,
               val title: Option[String] = None,
               knownSite: Option[String] = None
                )
{
  val site: String = knownSite.getOrElse(PostInfo.extractMainLevelSite(this.url))

  override def toString = url
}

object PostInfo
{
  def extractSite(url: String): String =
  {
    url.indexOf("://") match {
      case -1 => url.split('/').head
      case i => url.drop(i + 3).split('/').head
    }
  }

  def extractMainLevelSite(url: String): String =
  {
    val fullSite = extractSite(url)
    val domains = fullSite.split('.')
    domains.size match {
      case i if (i < 3) => fullSite
      case _ => domains.drop(domains.size - 2).mkString(".")
    }
  }
}

